Shader "DX11/Vertex Texture Test"
{
	Properties
	{
		_Texture("Texture", 2D) = "white" {}
	}
	SubShader
	{
		Pass
		{
			CGPROGRAM
			
			#pragma vertex vert
			#pragma fragment frag
			#pragma target 5.0
			
			#include "UnityCG.cginc"
			
			sampler2D _Texture;
			
			float unpackFloat4(float4 _packed)
			{
				float4 rgba = 255.0 * _packed;
				float sign =  step(-128.0, -rgba[1]) * 2.0 - 1.0;
				float exponent = rgba[0] - 127.0;    
				if (abs(exponent + 127.0) < 0.001){
					return 0.0;      
				} else {      
					float mantissa =  fmod(rgba[1], 128.0) * 65536.0 + rgba[2] * 256.0 + rgba[3] + (0x800000);
					return sign *  exp2(exponent-23.0) * mantissa ;     
				}
			}
			
			struct vs_input
			{
				float4 vertex : POSITION;
				float2 texcoord : TEXCOORD0;
			};
			
			struct ps_input
			{
				float4 pos : SV_POSITION;
				float2 uv : TEXCOORD0;
				float4 color : COLOR;
			};
			
			ps_input vert(vs_input v)
			{
				//float height = unpackFloat4(tex2Dlod(_Texture, float4(v.texcoord.xy, 0, 0)));

				float height = tex2Dlod(_Texture, float4(v.texcoord.xy, 0, 0)).r;
				float4 vert	= float4(v.vertex.x, height, v.vertex.z, 1);
				
				ps_input o;
				o.pos = mul(UNITY_MATRIX_MVP, vert);
				o.uv = v.texcoord.xy;
				o.color = float4(height, height, height, 1);
				return o;
			}
			
			float4 frag(ps_input i) : COLOR
			{
				return i.color;
			}
			
			ENDCG
		}
	}
	Fallback Off
}